package com.y.simple.filter;

import com.y.simple.global.ResultException;
import com.y.simple.utils.StringUtil;
import com.y.simple.utils.ThreadLocalUtil;
import com.y.simple.utils.TokenUtil;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class MyFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // 过滤器逻辑
        // 1 从请求中获得请求头的 token
        String token = ((HttpServletRequest) request).getHeader("Authorization");
        // 2 校验 token
        if (StringUtil.hasEmpty(token) && TokenUtil.checkToken(token)) {
            ThreadLocalUtil.add(TokenUtil.readToken(token));
            System.out.println("通过了");
        } else {
            System.out.println("没通过");
        }
        chain.doFilter(request, response);
    }
}